/**
 * 
 */
package com.vh.webapp.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.vh.model.Appointment;
import com.vh.service.AppointmentManager;

/**
 * @author Vijay Rajan
 *
 */
public class AppointmentAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private AppointmentManager appointmentManager;
	private List<Appointment> appointments;
	private Appointment appointment;
	private Long id;
	
	
	public void setAppointmentManager(AppointmentManager appointmentManager) {
		this.appointmentManager = appointmentManager;
	}

	public List<Appointment> getAppointments() {
		return appointments;
	}
	
	public String list() {
		appointments = appointmentManager.getAll();
		log.debug("appointment Size:"+ appointments.size());
        return SUCCESS;
    }
	
	public String delete() {
		appointmentManager.remove(new Long(appointment.getId()));
	    saveMessage(getText("appointment.deleted"));

	    return SUCCESS;
	}

	public String edit() {
	    if (id != null) {
	    	appointment = appointmentManager.get(id);
	    } else {
	    	appointment = new Appointment();
	    }

	    return SUCCESS;
	}


	public String save() throws Exception {
	    if (cancel != null) {
	        return CANCEL;
	    }

	    if (delete != null) {
	        return delete();
	    }

	    boolean isNew = (id == null);

	    appointment = appointmentManager.save(appointment);

	    String key = (isNew) ? "appointment.added" : "appointment.updated";
	    saveMessage(getText(key));

	    if (!isNew) {
	        return INPUT;
	    } else {
	        return SUCCESS;
	    }
	}
	
	public String saveAppointment(){
		appointmentManager.save(appointment);
		return SUCCESS;
	}
	
	
	/**
	 * Usage description: 
	 * 
	 * @see	com.celtic.cmvs
	 * @version 1.0 Jun 17, 2011
	 * @author Vijay Rajan
	 *
	 * @return the appointment
	 */
	public Appointment getAppointment() {
		return appointment;
	}

	/**
	 * Usage description: 
	 * 
	 * @see	com.celtic.cmvs
	 * @version 1.0 Jun 17, 2011
	 * @author Vijay Rajan
	 *
	 * @param appointment the appointment to set
	 */
	public void setAppointment(Appointment appointment) {
		this.appointment = appointment;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
